package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 礼物Controller
 *
 * @author 郁百超
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/index")
public class indexController extends BaseController
{
    @Autowired
    private ILivebroadcastingroomService livebroadcastingroomService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IViewRoomrecService viewRoomrecService;
    @Autowired
    private RemoteFileService remoteFileService;
    /**
     * 直播间推荐
     */

    @GetMapping("/hotlive")
    public TableDataInfo hotlive(ViewRoomrec viewRoomrec)
    {
        startPage();
        List<ViewRoomrec> list = viewRoomrecService.selectViewRoomrecList(viewRoomrec);
        return getDataTable(list);
    }

    /**
     * 用户推荐
     */
    @GetMapping("/hotuser")
    public TableDataInfo hotuser(UserInfo userInfo)
    {
        userInfo.setIsRecomend("1");
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 注册用户信息
     */
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody SysUser sysUser)
    {
        String username = sysUser.getUserName();
        //sysUser.setNickName(username);
        UserInfo userInfo = new UserInfo();
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return R.fail("当前系统没有开启注册功能！");
        }
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username)))
        {
            return R.fail("保存用户'" + username + "'失败，注册账号已存在");
        }
        //写入用户表
        userInfo.setPhone(username);
        userInfo.setCoin(Long.valueOf(0));
        userInfo.setUserName(sysUser.getNickName());
        userInfo.setIsRecomend("0");
        userInfo.setIsLive("0");
        userInfo.setUserType("0");

        userService.registerUser(sysUser);
        List<SysUser> sysUserList = userService.selectUserList(sysUser);
        userInfo.setUserId(sysUserList.get(0).getUserId());
        userInfoService.insertUserInfo(userInfo);
        return R.ok(true);
    }

    //图片上传
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }


}
